package com.lagou.edu.controller;

import com.lagou.edu.pojo.TbResume;
import com.lagou.edu.service.TbResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Optional;

@Controller
@RequestMapping("resume")
public class TbResumeController {


    @Autowired
    private TbResumeService tbResumeService;


    /**
     * 列表
     * @param model
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Model list(Model model){
        List<TbResume> allResume = tbResumeService.findAll();
        model.addAttribute("list", allResume);
        return model;
    }

    @RequestMapping("toSave")
    public String toSave(Model model) {
        model.addAttribute("action","save");
        return "resume/saveOrUpdate";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Long id,Model model) {
        Optional<TbResume> resumeOptional = tbResumeService.findById(id);
        model.addAttribute("action","update");
        model.addAttribute("resume",resumeOptional.get());
        return "resume/saveOrUpdate";
    }


    /**
     * 新增
     * @param resume
     * @return
     */
    @RequestMapping("saveResume")
    public String save(TbResume resume) {
        tbResumeService.saveOrUpdateResume(resume);
        return "redirect:list";
    }


    /**
     * 更新
     * @param resume
     * @return
     */
    @RequestMapping("updateResume")
    public String update(TbResume resume) {
        tbResumeService.saveOrUpdateResume(resume);
        return "redirect:list";
    }


    /**
     * 删除
     * @param resume
     * @return
     */
    @RequestMapping("del")
    public String del(TbResume resume) {
        tbResumeService.deleteResume(resume);
        return "redirect:list";
    }

}
